<?php 
class EComUtils {
  public static function update_cart($myPdo, $cart, $customer) {
    foreach ($cart as $idx => $pur) {
      $pur->_stock = 0;
      $cond_vals = new stdClass(); 
      $cond_vals->c = 't.track_id = :v1';
      $cond_vals->v = array(':v1' => $pur->product_track_id);
      $opt = new stdClass();
      $opt->select_expr = 'id, track_id, date, enabled_stock, enabled_delivery, enabled_member_price, brand_id, category_id, name, price, stock, stock_alert, intro, status, sold__, alert__';
      $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;
      if (count($tmp) > 0) {
        $pur->_enabled_stock = $tmp[0]->enabled_stock;
        $pur->_enabled_delivery = $tmp[0]->enabled_delivery;
        if ($tmp[0]->enabled_stock == 1) {
          $pur->_stock = $tmp[0]->stock;
        }
        $pur->price = $tmp[0]->price;
        if ($customer) {
          $cond_vals = new stdClass(); 
          $cond_vals->c = 't.product_track_id = :v1 AND t.member_id = :v2';
          $cond_vals->v = array(':v1' => $pur->product_track_id, ':v2' => $customer->member_id);
          $tmp2 = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_member_price', $cond_vals,  NULL, NULL, NULL, NULL, NULL)->d;
          if (count($tmp2) > 0) {
            $pur->price = $tmp2[0]->price;
          } 
        }
        $cond_vals = new stdClass(); 
        $cond_vals->c = 't.product_track_id = :v1 AND t.attr_opt_id = :v2 AND t.attr_opt_id != \'\'';
        $cond_vals->v = array(':v1' => $pur->product_track_id, ':v2' => $pur->attr_opt_id);
        $tmp2 = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $cond_vals,  NULL, NULL, NULL, NULL, NULL)->d;
        if (count($tmp2) > 0) {
          if ($tmp[0]->enabled_stock == 1) {
            $pur->_stock = $tmp2[0]->stock;
          }
          $pur->price = sprintf("%01.2f", $pur->price + $tmp2[0]->diff_price);
        }
        $pur->total = sprintf("%01.2f", $pur->price * $pur->quantity);
      }
    }
    setcookie('my_cart', json_encode($cart), time() + 86400 * 7);
  }
  
  public static function update_stock($myPdo, $order, $is_cancel) {
    $cond_vals = new stdClass(); 
    $cond_vals->c = 't.order_id = :v1';
    $cond_vals->v = array(':v1' => $order->id);
    $purchases = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_purchase', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    foreach ($purchases as &$pur) {
      if ($pur->attr_opt_id != '') {
        $cond_vals = new stdClass(); 
        $cond_vals->c = 't.product_track_id = :v1 AND t.attr_opt_id = :v2';
        $cond_vals->v = array(':v1' => $pur->product_track_id, ':v2' => $pur->attr_opt_id);
        $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
        if (count($tmp) > 0) {
          if ($is_cancel == 0) {
            $tmp[0]->stock -= $pur->quantity;
          } else {
            $tmp[0]->stock += $pur->quantity;
          }
          DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product_object', $tmp[0]);
          $cond_vals = new stdClass(); 
          $cond_vals->c = 't.track_id = :v1';
          $cond_vals->v = array(':v1' => $pur->product_track_id);
          $opt = new stdClass();
          $opt->select_expr = 'id, track_id, date, enabled_stock, enabled_delivery, enabled_member_price, brand_id, category_id, name, price, stock, stock_alert, intro, status, sold__, alert__';
          $products = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;
          if (count($products) > 0) {
            if ($is_cancel == 0) {
              $products[0]->sold__ += $pur->quantity;
            } else {
              $products[0]->sold__ -= $pur->quantity;
            }
            DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $products[0]);
            EComUtils::update_alert($myPdo, $products[0], $tmp);
          }
        }
      } else {
        $cond_vals = new stdClass(); 
        $cond_vals->c = 't.track_id = :v1';
        $cond_vals->v = array(':v1' => $pur->product_track_id);
        $opt = new stdClass();
        $opt->select_expr = 'id, track_id, date, enabled_stock, enabled_delivery, enabled_member_price, brand_id, category_id, name, price, stock, stock_alert, intro, status, sold__, alert__';
        $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;
        if (count($tmp) > 0) {
          if ($is_cancel == 0) {
            $tmp[0]->stock -= $pur->quantity;
            $tmp[0]->sold__ += $pur->quantity;
          } else {
            $tmp[0]->stock += $pur->quantity;
            $tmp[0]->sold__ -= $pur->quantity;
          }
          DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $tmp[0]);
          EComUtils::update_alert($myPdo, $tmp[0], NULL);
        }
      }
    }
  }
  
  public static function update_alert($myPdo, $product, $objects) {
    if (!$objects) {
      $product->alert__ = ($product->stock <= $product->stock_alert);
      DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $product);
    } else {
      foreach ($objects as $obj) {
        if ($obj->stock <= $obj->stock_alert && $obj->stock_alert != 0) {
          $product->alert__ = 1;
          DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $product);
          return;
        }
      }
      $product->alert__ = 0;
      DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $product);
    }
  }
  
  public static function update_customer($myPdo, $customer_id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_id = :v1 AND is_paid = 1 AND is_cancel = 0';
    $cond_vals->v = array(':v1' => $customer_id);
    $opt = new stdClass();
    $opt->select_expr = 'SUM(t.total) AS _total';
    $total = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
    if (!$total) {
      $total = 0;
    }
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.total_spent <= :v1';
    $cond_vals->v = array(':v1' => $total);
    $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_member', $cond_vals, 't.total_spent', 'DESC', 1, 1, NULL)->d;
    
    if (count($tmp) > 0 && $customer_id != 0) {
      $obj = new stdClass();
      $obj->id = $customer_id;
      $obj->member_id = $tmp[0]->id;
      DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $obj);
    }
  }
}
?>
